var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
(function (factory) {
    if (typeof module === "object" && typeof module.exports === "object") {
        var v = factory(require, exports);
        if (v !== undefined) module.exports = v;
    }
    else if (typeof define === "function" && define.amd) {
        define(["require", "exports", "./common", "./services/local-service", "json!websiteConfig", "controls/component-loader"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    const common_1 = require("./common");
    const local_service_1 = require("./services/local-service");
    const json_websiteConfig_1 = require("json!websiteConfig");
    const component_loader_1 = require("controls/component-loader");
    function default_1(args) {
        return __awaiter(this, void 0, void 0, function* () {
            window[common_1.ADMIN_APP] = args.app;
            let localService = args.app.createService(local_service_1.LocalService);
            let files = yield localService.clientFiles();
            args.app.pageCreated.add((sender, page) => {
                let className = page.name.split("/").filter(o => o != "").join("-");
                page.element.className = `admin-page ${className}`;
                let lessFilePath;
                if (page.name.indexOf(":")) {
                    let arr = page.name.split(":");
                    lessFilePath = `modules/${arr[1]}.less`;
                }
                else {
                    lessFilePath = `modules/${page.name}.less`;
                }
                let pageLessFiles = files.filter(o => o.startsWith("modules") && o.endsWith(".less"));
                if (pageLessFiles.indexOf(lessFilePath) >= 0) {
                    common_1.loadLess(lessFilePath);
                }
            });
            requirejs({ context: json_websiteConfig_1.contextName }, ['less!site']);
            requirejs({ context: json_websiteConfig_1.contextName }, ['qrcode', 'clipboard'], function (qrcode, clipboard) {
                window['QRCode'] = qrcode || window['QRCode'];
                window['ClipboardJS'] = clipboard || window['ClipboardJS'];
            });
        });
    }
    exports.default = default_1;
    //=============================================================
    // 为减少后期组件加载的时间，同时为了不影响当前加载，延时加载所有组件
    setTimeout(() => {
        component_loader_1.ComponentLoader.loadAllComponents();
    }, 1000 * 60);
    //=============================================================
    function loadEditors(localService) {
        return __awaiter(this, void 0, void 0, function* () {
            let files = yield localService.clientFiles();
            let editorFiles = files.filter(o => o.startsWith("components") && o.endsWith(".js") && o.indexOf("editor") >= 0);
            editorFiles.forEach(path => requirejs({ context: json_websiteConfig_1.contextName }, [path]));
        });
    }
    function loadLessFiles(localService) {
        return __awaiter(this, void 0, void 0, function* () {
            let files = yield localService.clientFiles();
            let editorLessFiles = files.filter(o => o.startsWith("components") && o.endsWith("editor.less"));
            editorLessFiles.forEach(path => {
                common_1.loadLess(path);
            });
        });
    }
});
